<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工位" prop="station">
        <el-input
          v-model="queryParams.station"
          placeholder="请输入工位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数模焊点号" prop="sjNumber">
        <el-input
          v-model="queryParams.sjNumber"
          placeholder="请输入焊点号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机器人名称" prop="Robot">
        <el-select
          v-model="queryParams.robot"
          placeholder="请选择机器人型号"
          clearable
          @keyup.enter.native="handleQuery"
          @change="handleSelectChange" >
          <el-option
            v-for="item in midList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规范号" prop="gaugeNumber">
        <el-input
          v-model="queryParams.gaugeNumber"
          placeholder="请输入规范号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="材质1" prop="material1">
        <el-input
          v-model="queryParams.material1"
          placeholder="请输入材质1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质1厚度(mm)" prop="materialThickness1">
        <el-input
          v-model="queryParams.materialThickness1"
          placeholder="请输入材质1厚度(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涂胶1" prop="gumming1">
        <el-input
          v-model="queryParams.gumming1"
          placeholder="请输入涂胶1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质2" prop="material2">
        <el-input
          v-model="queryParams.material2"
          placeholder="请输入材质2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质2厚度(mm)" prop="materialThickness2">
        <el-input
          v-model="queryParams.materialThickness2"
          placeholder="请输入材质2厚度(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涂胶2" prop="gumming2">
        <el-input
          v-model="queryParams.gumming2"
          placeholder="请输入涂胶2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质3" prop="material3">
        <el-input
          v-model="queryParams.material3"
          placeholder="请输入材质3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质3厚度(mm)" prop="materialThickness3">
        <el-input
          v-model="queryParams.materialThickness3"
          placeholder="请输入材质3厚度(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涂胶3" prop="gumming3">
        <el-input
          v-model="queryParams.gumming3"
          placeholder="请输入涂胶3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质4" prop="material4">
        <el-input
          v-model="queryParams.material4"
          placeholder="请输入材质4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质4厚度(mm)" prop="materialThickness4">
        <el-input
          v-model="queryParams.materialThickness4"
          placeholder="请输入材质4厚度(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Group:group:export']"
        >导出2表</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport1"
          v-hasPermi="['Group:group:export']"
        >导出总表</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport2"
          v-hasPermi="['Group:group:export']"
        >导出总表2</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="Dump"
          v-hasPermi="['Group:group:export']"
        >转存</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="weldList"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%;"
      :show-overflow-tooltip="true"
      empty-text="暂无数据"
      :cell-style="cellStyle"
    >
    <el-table-column type="selection" align="center" width="55"></el-table-column>
    <el-table-column label="序号" type="index" align="center">
      <template slot-scope="scope">
        <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
      </template>
    </el-table-column>
      <el-table-column label="控制器类型" align="center" prop="timertype" />
      <el-table-column label="控制器所在区域" align="center" prop="timerarea" />
      <el-table-column label="控制器名称" align="center" prop="timername" />
      <el-table-column label="控制器ID" align="center" prop="timerid" />
      <el-table-column label="焊点索引" align="center" prop="wldindex" />
      <el-table-column label="VIN" align="center" prop="vin" />
      <el-table-column label="车型码" align="center" prop="vehiclecode" />
      <el-table-column label="焊点码" align="center" prop="spotcode" />
      <el-table-column label="规范" align="center" prop="sch" />
      <el-table-column label="焊点名称" align="center" prop="spotname" />
      <el-table-column label="电极号" align="center" prop="tipn" />
      <el-table-column label="时间" align="center" prop="time" />
      <el-table-column label="资料错误码" align="center" prop="fault" />
      <el-table-column label="预热电流" align="center" prop="wld1c" />
      <el-table-column label="预热时间" align="center" prop="wld1t" />
      <el-table-column label="焊接电流" align="center" prop="wld2c" />
      <el-table-column label="焊接时间" align="center" prop="wld2t" />
      <el-table-column label="回火电流" align="center" prop="wld3c" />
      <el-table-column label="回火时间" align="center" prop="wld3t" />
      <el-table-column label="电极电压1" align="center" prop="tipv1" />
      <el-table-column label="电容电压1" align="center" prop="capv1" />
      <el-table-column label="导通比1" align="center" prop="ron1" />
      <el-table-column label="电极电压2" align="center" prop="tipv2" />
      <el-table-column label="电容电压2" align="center" prop="capv2" />
      <el-table-column label="导通比2" align="center" prop="ron2" />
      <el-table-column label="电极电压3" align="center" prop="tipv3" />
      <el-table-column label="电容电压3" align="center" prop="capv3" />
      <el-table-column label="导通比3" align="center" prop="ron3" />
      <el-table-column label="步指针" align="center" prop="stpp" />
      <el-table-column label="步计数" align="center" prop="stpn" />
      <el-table-column label="修磨计数" align="center" prop="tdress" />
      <el-table-column label="预留" align="center" prop="cool2" />
      <el-table-column label="预留" align="center" prop="pulse2" />
      <el-table-column label="电阻" align="center" prop="wres" />
      <el-table-column label="预留" align="center" prop="frequency" />
      <el-table-column label="能量" align="center" prop="energy" />
      <el-table-column label="预留" align="center" prop="evalueCode" />
      <el-table-column label="飞溅评价码" align="center" prop="spattercode" />
      <el-table-column label="焊接飞溅位置" align="center" prop="spatterPosWld" />
      <el-table-column label="预热飞溅位置" align="center" prop="spatterPosPre" />
      <el-table-column label="回火飞溅位置" align="center" prop="spatterPosTmp" />
      <el-table-column label="预留" align="center" prop="evaluestatus" />
      <el-table-column label="群控主机IP" align="center" prop="stationip" />
      <el-table-column label="焊接曲线存放路径" align="center" prop="cvepath" />
      <el-table-column label="预留" align="center" prop="spotinf" />
      <el-table-column label="预留" align="center" prop="combinecode" />
      <el-table-column label="预留" align="center" prop="weldgun" />
      <el-table-column label="预留" align="center" prop="pressure" />
      <el-table-column label="预留" align="center" prop="extradata" />
      <el-table-column label="预留" align="center" prop="aiRlt" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>


    <el-dialog :title="Export.title" :visible.sync="Export.open" width="400px" append-to-body>
      <span>是否导出全部数据</span>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="ExportAll">确 定</el-button>
        <el-button @click="Export.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="Export1.title" :visible.sync="Export1.open" width="400px" append-to-body>
      <span>是否存储数据</span>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dump">确 定</el-button>
        <el-button @click="Export1.open = false">取 消</el-button>
      </div>
    </el-dialog>
</div>
</template>

<script>
import { listGroup, getGroup} from "@/api/Group/group";
import { getToken } from "@/utils/auth";
import axios from "axios";

export default {
  name: "GroupControl",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //下拉框存储的数据
      midList:[],
      // 存储用户选择的数据项
      multipleSelection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch:false,
      // 总条数
      total: 0,
      // 表数据
      weldList: [],
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      //导入文件的名称
      importFileName: null,

      // 导出参数
      Export:{
        // // 是否显示弹出层
        open: false,
        // // 弹出层标题
        title: "",
      },
      // 导出参数
      Export1:{
        // // 是否显示弹出层
        open: false,
        // // 弹出层标题
        title: "",
      },
      //分页参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        ipaddr: undefined,
        name:undefined,
        tableName: undefined,
        tableComment: '' // 假设这是下拉列表绑定的模型
      },
    };
  },
  //页面自动执行的方法
  created() {
    this.getList();
    this.queryMid();
  },

  methods: {
    //查询表格数据
    getList(){
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        this.weldList = response.rows;
        this.total= response.total;
        this.loading = false;
      });
    },

    //机器人搜索下拉框
    queryMid(){
      listGroup(this.queryParams).then(response => {
        const result = response.rows;
        result.forEach(element=>{
          const robot=element.robot;
          const isDuplicate=this.midList.some(item=>item.label===robot && item.value===robot);
          if (!isDuplicate){
          this.midList.push({label:element.robot,value:element.robot});
          }
        })
        console.log(this.midList);
      }).catch(err=>{
        console.log(err);
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.multipleSelection =selection;
    },

    /** 导出小表按钮操作 */
    handleExport() {
      if (this.multipleSelection.length===0){
        this.$message({
          message:"请选择导出数据",
          type: "warning",
        });
        return ;
      }
      const ids=this.multipleSelection.map(item => item.id).join(',');
      this.download('Group/group/export1', {ids}, `weld_${new Date().getTime()}.xlsx`)
      this.download('Group/group/export2', {ids}, `weld_${new Date().getTime()}.xlsx`)
    },

    /** 导出总表按钮操作 */
    handleExport1() {
      if (this.multipleSelection.length===0){
        this.Export.title = "数据导入";
        this.Export.open = true;
      }else {
        const ids = this.multipleSelection.map(item => item.id).join(',');
        this.download('Group/group/export3', {ids}, `${this.importFileName}`)
      }
    },
    ExportAll(){
      this.download('Group/group/exportAll',1,`${this.importFileName}`);
      this.Export.open = false;
    },
    //导出总表2
    handleExport2(){
      this.download('Group/group/exportAll2',1,`${this.importFileName}`);

    },

    /** 转存 */
    Dump(){
      this.Export1.title = "数据存储";
      this.Export1.open = true;
    },
    dump(row){
      // 判断是否有选中的数据
      if (this.multipleSelection.length > 0) {
        // 如果有选中的数据，保存选中的数据到另一个表
        const Ids = row.ID || this.ids;
        let selectedIds = Ids;
        console.log(Ids);
        this.saveToAnotherTable(selectedIds, true); // true 表示只保存选中的数据
      } else {
        // 如果没有选中的数据，保存全部数据到另一个表
        this.saveToAnotherTable(null, false); // false 表示保存全部数据
      }
      this.Export1.open = false; // 关闭弹窗
    },
    saveToAnotherTable(ids, isPartial) {
      // 定义接口地址
      const url = isPartial ? '/dev-api/DataStation/dataStation/saveSelected' : '/dev-api/DataStation/dataStation/saveAll';
      // 调用接口
      axios.post(url, {
        ids: ids // 如果 isPartial 为 true，ids 是选中的数据 ID 列表；否则为 null
      },{
        headers: {
          // 将 token 添加到请求头中，通常是 'Authorization' 头
          'Authorization': `Bearer ${getToken()}`
        }
      }).then(response => {
        this.$modal.msgSuccess("转存成功！");
        // 触发事件，通知第二个页面刷新
        EventBus.$emit('refreshAnotherTable');
      }).catch(err => {
        this.$modal.msgError("转存失败：" + err.message);
      });
    },

    //搜索框里面的下拉的数据选择
    handleSelectChange(newValue) {
      // 当选择的值变化时，这里将接收新的值作为参数
      console.log('选中的值变化为:', newValue);
      // 在这里可以执行进一步的逻辑处理
    },
  }
};
</script>

